﻿using SimplyCleverMiddleTier.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimplyCleverMiddleTier
{
    public static partial class CountryProvider
    {
        public static void Save(Address address)
        {
            Db.ReadFrom("SimplyClever", false,
                command =>
                {
                    command.CommandText = "INSERT INTO UserAddresses(Line1, Line2, City, StateCode, CountryCode, County, ZipCode) VALUES(@Line1, @Line2, @City, @StateCode, @CountryCode, @County, @ZipCode)";
                    command.AddParameter("@Line1", address.Line1);
                    command.AddParameter("@Line2", address.Line2 ?? string.Empty);
                    command.AddParameter("@County", address.County ?? string.Empty);
                    command.AddParameter("@City", address.City);
                    command.AddParameter("@StateCode", address.StateCode ?? string.Empty);
                    command.AddParameter("@CountryCode", address.CountryCode);
                    command.AddParameter("@ZipCode", address.ZipCode);
                },
                reader =>
                {
                });
        }

        public static UserAddress[] GetUserAddresses()
        {
            List<UserAddress> list = new List<UserAddress>();

            Db.ReadFrom("SimplyClever", false,
                command =>
                {
                    command.CommandText = "SELECT * FROM UserAddresses ORDER BY Created DESC";
                },
                reader =>
                {
                    Address address = new Address();
                    address.Line1 = reader.GetValue<string>("Line1");
                    address.Line2 = reader.GetValue<string>("Line2");
                    address.County = reader.GetValue<string>("County");
                    address.City = reader.GetValue<string>("City");
                    address.ZipCode = reader.GetValue<string>("ZipCode");
                    address.StateCode = reader.GetValue<string>("StateCode");
                    address.CountryCode = reader.GetValue<string>("CountryCode");

                    UserAddress userAddress = new UserAddress();
                    userAddress.Address = address;
                    userAddress.LastModified = reader.GetValue<DateTime>("Created");

                    list.Add(userAddress);
                });

            return list.ToArray();
        }


    }
}
